﻿
var operateList = angular.module('operateHome', ['Common', 'personnelmanagementS', 'loadModule'])
.controller('ophome', ['$scope', '$location', 'RequestPopService', 'Msg', 'MsgCon', 'sysUserInfo', "T", function ($scope, $location, OS, Msg, MsgCon, sysUserInfo, T) {

    $scope.url = $location.path().split('/').join('.');
    $scope.url = $scope.url.substr(1, $scope.url.length - 1).toLocaleLowerCase();
    $scope.isTrain = true;
    if ($scope.url == "index.train.home") {
        $scope.isTrain = true;
    } else {
        $scope.isTrain = false;
    }
    $scope.topTeacher = [];
    //活跃度参数
    $scope.params = {
        pageSize: "5",
        stateTime: "",
        endTime: "",
        powerLV: sysUserInfo.getuserinfo().powerLV,
        user_ID: sysUserInfo.getuserinfo().user_ID,
        organization_ID: sysUserInfo.getuserinfo().organization_ID,
        searchOrgIds: ""
    };
    // 给时间赋值
    $scope.GetDate = function () {
        $scope.params.stateTime = document.getElementById("startDate").value;
        $scope.params.endTime = document.getElementById("endDate").value;
    }
    //默认隐藏溢出
    $scope.loadMore = false;

    //展开或关闭更多企业
    $scope.loadMoreOrgList = function () {
        //展开
        if (!$scope.loadMore) {
            $scope.smallOrgList = angular.copy($scope.orgList);
            //关闭
        } else {
            $scope.smallOrgList = angular.copy($scope.orgList);
            if ($scope.orgList.length > 8)
                $scope.smallOrgList.length = 8;
        }
        $scope.loadMore = !$scope.loadMore;

    }
    //全部的单击事件
    $scope.selectAllOrg = function () {
        $scope.params.searchOrgIds = "";
        //大集合中已选中的企业
        angular.forEach($scope.orgList, function (data, index, array) {
            data.boss = false;
            $scope.smallOrgList[index].boss = false;
        });
        //请求企业
        $scope.initFun();
    }
    //选中企业
    $scope.checkOrgName = function (item) {
        $scope.params.searchOrgIds = "";
        item.boss = !item.boss;
        //大集合中已选中的企业
        angular.forEach($scope.orgList, function (data, index, array) {
            if (data.organization_ID == item.organization_ID) {
                //小集合选中，则选中
                if (item.boss) {
                    data.boss = true;
                    //否则，取消选中
                } else {
                    data.boss = false;
                }
            }
        });
        //请求企业
        $scope.onLoadOrg();

    }
    //筛选企业的统计
    $scope.onLoadOrg = function () {
        angular.forEach($scope.orgList, function (data, index, array) {
            if (data.boss) {
                data.check = false;
                $scope.params.searchOrgIds += ($scope.params.searchOrgIds == "") ? data.organization_ID : "," + data.organization_ID;
            }
        });

        $scope.initFun();
        //console.log($scope.params.searchOrgIds)
    }
    $scope.smallOrgList = [];
    //获取企业列表
    $scope.getorgList = function () {
        OS.searchOrgList({ userId: sysUserInfo.getuserinfo().user_ID }).then(function (response) {
            $scope.operateLoading = false;  //关闭loading
            if (response.errorcode == "0") {// 成功       
                $scope.orgList = response.datas;
                $scope.smallOrgList = angular.copy($scope.orgList);
                if ($scope.orgList.length > 8)
                    $scope.smallOrgList.length = 8;
            } else {   // 获取失败 
                Msg.error(T.T("DataAccessFailed"))
            }
        }, function errorCallback() {
            //console.log(T.T("RequestException"));
        });
    }

    //获取企业列表
    //$scope.getorgList();
    //*优秀教师TOP5    
    $scope.GetTopTeacher = function () {
        $scope.operateLoading = true;   // 开启loading       
        OS.findStatisticsRankByExTeacher($scope.params).then(function (response) {
            $scope.operateLoading = false;  //关闭loading
            if (response.errorcode == "0") {// 成功       
                $scope.topTeacher = response.datas;
            } else {   // 获取失败 
                Msg.error(T.T("DataAccessFailed"))
            }
            //初始创建的课程数量
            //统一样式
            if ($scope.topTeacher != undefined) {
                $scope.topTeacher.forEach(function (data, index) {
                    if (data.createCourseNum == undefined) {
                        data.createCourseNum = 0;
                    }
                });
            }
        }, function errorCallback() {
            //console.log(T.T("RequestException"));
        });
    }

    $scope.topCourse = [];
    //获取课程观看量统计排名信息
    $scope.GetTopCourse = function () {
        $scope.operateLoading = true;   // 开启loading       
        OS.findStatisticsRankByCourse($scope.params).then(function (response) {
            //            //console.log(response.datas);
            $scope.operateLoading = false;  //关闭loading
            if (response.errorcode == "0") {// 成功       
                $scope.topCourse = response.datas;
                var datasource = response.datas;
                var dom = document.getElementById("container2");
                var myChart = echarts.init(dom);
                var app = {};
                option = null;


                var category = [];
                var dottedBase = +new Date();
                var lineData = [];
                var barData = [];
                /*
                for (var i = 0; i < 20; i++) {
                var date = new Date(dottedBase += 1000 * 3600 * 24);
                category.push([
                date.getFullYear(),
                date.getMonth() + 1,
                date.getDate()
                ].join('-'));
                var b = Math.random() * 200;
                var d = Math.random() * 200;
                barData.push(b)
                lineData.push(d + b);
                }
                */
                if (datasource != undefined) {
                    for (var i = 0; i < datasource.length; i++) {
                        category.push(datasource[i].course_Name);
                        barData.push(datasource[i].accessLearningTime);
                        lineData.push(datasource[i].accessNum);
                    }
                }

                option = {
                    backgroundColor: '#414961',
                    title: {
                        text: T.T("DurationAndAccessOfCourseLearning"),
                        textStyle: {
                            fontWeight: 'normal',
                            fontSize: 16,
                            color: '#F1F1F3'
                        },
                        left: '6%'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow',
                            label: {
                                show: true,
                                backgroundColor: '#333'
                            }
                        }
                    },
                    legend: {
                        data: [T.T("TotalLearningDuration"), T.T("UserAccess")],
                        textStyle: {
                            color: '#ccc'
                        }
                    },
                    xAxis: {
                        data: category,
                        axisLine: {
                            lineStyle: {
                                color: '#ccc'
                            }
                        }
                    },
                    yAxis: {
                        splitLine: { show: false },
                        axisLine: {
                            lineStyle: {
                                color: '#ccc'
                            }
                        }
                    },

                    series: [{
                        name: T.T("TotalLearningDuration"),
                        type: 'line',
                        smooth: true,
                        showAllSymbol: true,
                        symbol: 'emptyCircle',
                        symbolSize: 12,
                        data: barData,
                        lineStyle: {
                            normal: {
                                width: 1,
                                color: '#ff5555'
                            }
                        }
                        //color: 'rgba(219, 50, 51, 0)'
                    }, {
                        name: T.T("UserAccess"),
                        type: 'bar',
                        barWidth: 10,
                        itemStyle: {
                            normal: {
                                barBorderRadius: 5,
                                color: new echarts.graphic.LinearGradient(
        0, 0, 0, 1,
        [
        { offset: 0, color: '#14c8d4' },
        { offset: 1, color: '#43eec6' }
        ]
        )
                            }
                        },
                        data: lineData
                    }]
                };
                if (option && typeof option === "object") {
                    myChart.setOption(option, true);
                }

            } else {   // 获取失败 
                Msg.error(T.T("DataAccessFailed"))
            }
        }, function errorCallback() {
            //console.log(T.T("RequestException"));
        });
    }

    $scope.topOrg = [];
    //*获取企业活跃度统计排名信息
    $scope.GetTopOrg = function () {
        $scope.operateLoading = true;   // 开启loading    
        if ($scope.isTrain) {  // 课程首页
            OS.findBaseDepartmentActivity({ userid: sysUserInfo.getuserinfo().user_ID, pageIndex: 1, pageSize: 5 }).then(function (response) {
                $scope.operateLoading = false;  //关闭loading          
                if (response.errorcode == "0") {// 成功       
                    $scope.topOrg = response.datas;
                    var newName = [];
                    var newData = [];
                    var newjson = response.datas; // eval(json1);
                    newjson.forEach(function (val, index, arr) {
                        if (val.departmentName == undefined) {
                            newName.push("异常客户" + (index + 1));
                            newData.push({ value: val.accessNum, name: "异常客户" + (index + 1) });
                        } else {
                            if (val.departmentName.length > 7) {
                                val.departmentName = val.departmentName.substr(0, "7") + "\n" + val.departmentName.substr(7, val.departmentName.length);
                            }
                            newName.push(val.departmentName);
                            newData.push({ value: val.accessNum, name: val.departmentName });
                        }
                    });
                    $scope.rendTopOrg(newName, newData, T.T("EnterpriseActivityTOP5"), T.T("DepartmentLoginTimes") + "/TOP5" + T.T("TotalNumberOfTimes") + " * 100%");
                } else {
                    Msg.error(T.T("DataAccessFailed"))
                }
            });
        } else {
            OS.findStatisticsRankByCompanyActivity($scope.params).then(function (response) {
                $scope.operateLoading = false;  //关闭loading            
                if (response.errorcode == "0") {// 成功       
                    $scope.topOrg = response.datas;
                    var newName = [];
                    var newData = [];
                    var newjson = response.datas; // eval(json1);
                    newjson.forEach(function (val, index, arr) {
                        //没有名字的就不展示
                        if (val.companyName == undefined) {
//                            newName.push("异常客户" + (index + 1));
//                            newData.push({ value: val.accessNum, name: "异常客户" + (index + 1) });
                        } else {
                            if (val.companyName.length > 7) {
                                val.companyName = val.companyName.substr(0, "7") + "\n" + val.companyName.substr(7, val.companyName.length);
                            }
                            newName.push(val.companyName);
                            newData.push({ value: val.accessNum, name: val.companyName });
                        }
                    });
                    $scope.rendTopOrg(newName, newData, T.T("EnterpriseActivityTOP5"), T.T("TotalNumberOfEnterprisePersonnelLoggingIn") + "/TOP5" + T.T("TotalNumberOfTimes") + "  * 100%");
                } else {   // 获取失败 
                    Msg.error(T.T("DataAccessFailed"))
                }
            }, function errorCallback() {
                //console.log(T.T("RequestException"));
            });
        }
    }

    // 渲染企业活跃度统计排名
    $scope.rendTopOrg = function (newName, newData, title, txt) {
        var dom = document.getElementById("toporg");
        var myChart = echarts.init(dom);
        option = null;
        //饼状图
        option = {
            title: {
                text: title,
                //subtext: T.T("TotalNumberOfEnterprisePersonnelLoggingIn") + '／' + T.T("NumberOfEnterprises") + '＊100%',
                subtext: txt, // '企业登录次数/TOP5总次数 * 100%'
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                x: 'center',
                y: 'bottom',
                data: newName
            },
            toolbox: {
                show: false,
                feature: {
                    mark: { show: false },
                    dataView: { show: false, readOnly: false },
                    magicType: {
                        show: false,
                        type: ['pie', 'funnel']
                    },
                    restore: { show: false },
                    saveAsImage: { show: false }
                }
            },
            calculable: true,
            series: [{
                name: T.T("EnterpriseActivityTOP5"),
                type: 'pie',
                radius: [30, 110],
                center: ['50%', '50%'],
                roseType: 'area',
                data: newData
            }
        ]
        };
        if (option && typeof option === "object") {
            myChart.setOption(option, true);
        }

    }
    /******************************************导出********************************************************/
    $scope.excelUrlA = "";
    $scope.exportData = function () {
        var paramExcel = angular.copy($scope.params);
        paramExcel.type = 2;
        OS.findStatisticsRankByProActivity(paramExcel).then(function (data) {
            if (data.errorcode == "0" && data.errormsg != undefined && data.errormsg.length > 0) {
                window.location = data.errormsg;
            }
            else if (data.errorcode == "73" || (data.errorcode == "0" && (data.errormsg == undefined || data.errormsg.length <= 0))) {
                setTimeout(function () {
                    $scope.exportData();
                }, 5000);
                Msg.warning(T.T("StatisticsAreBeingProcessed"));
            } else {
                Msg.error(T.T("RequestDataError"));
            }
        });
    }

    /******************************************查询*********************************************************/

    //*获取项目访问活跃度趋势
    $scope.GetAllTopActivity = function () {
        $scope.operateLoading = true;   // 开启loading       
        OS.findStatisticsRankByProActivity($scope.params).then(function (response) {
            //            //console.log(response);
            $scope.operateLoading = false;  //关闭loading            
            if (response.errorcode == "0") {// 成功       
                $scope.topOrg = response.datas;


                var dom = document.getElementById("container");
                var myChart = echarts.init(dom);
                var app = {};
                option = null;



                //var json2 = '[{ "value": "10", "data": "6-1" },{ "value": "200", "data": "2日" },{ "value": "14", "data": "3日" },{ "value": "18", "data": "4日" },{ "value": "81", "data": "5日" },{ "value": "311", "data": "6日" },{ "value": "177", "data": "7日" },{ "value": "12", "data": "8日" },{ "value": "1", "data": "9日" },{ "value": "201", "data": "10日" },{ "value": "102", "data": "11日" },{ "value": "200", "data": "12日" },{ "value": "15", "data": "13日" },{ "value": "1", "data": "14日" },{ "value": "103", "data": "15日" },{ "value": "31", "data": "16日" },{ "value": "18", "data": "17日" },{ "value": "14", "data": "18日" },{ "value": "221", "data": "7-19" },{ "value": "109", "data": "20日" },{ "value": "111", "data": "21日" },{ "value": "17", "data": "22日" },{ "value": "41", "data": "23日" },{ "value": "441", "data": "24日" },{ "value": "111", "data": "25日" },{ "value": "301", "data": "26日" },{ "value": "101", "data": "27日" },{ "value": "81", "data": "28日" },{ "value": "101", "data": "29日" },{ "value": "1", "data": "30日" },]';
                var newName = [];
                // 登录活跃量
                var newData = [];
                // 注册活跃量
                var registerData = [];
                var newjson = [];
                if (response.datas != undefined) {
                    var newjson = response.datas; // eval(json2);
                }
                newjson.forEach(function (val, index, arr) {
                    newName.push(val.activityDate);
                    newData.push(val.accessNum);
                    registerData.push(val.registerNum ? val.registerNum : 0);
                });


                option = {
                    backgroundColor: '#394056',
                    title: {
                        text: T.T("PlatformAccessAnalysis"),
                        textStyle: {
                            fontWeight: 'normal',
                            fontSize: 16,
                            color: '#F1F1F3'
                        },
                        left: '6%'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            lineStyle: {
                                color: '#57617B'
                            }
                        }
                    },
                    legend: {
                        icon: 'rect',
                        itemWidth: 14,
                        itemHeight: 5,
                        itemGap: 13,
                        data: [

                        {
                            name: T.T("UserAccess"),
                            textStyle: {
                                fontSize: 12,
                                color: '#F1F1F3'
                            }
                        }, {
                            name: T.T("RegistrationAmount"),
                            textStyle: {
                                fontSize: 12,
                                color: '#F1F1F3'
                            }
                        }

                        ], //, '桂林-企业', '南宁-企业'
                        right: '4%',
                        textStyle: {
                            fontSize: 12,
                            color: '#F1F1F3'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [{
                        type: 'category',
                        boundaryGap: false,
                        axisLine: {
                            lineStyle: {
                                color: '#57617B'
                            }
                        },
                        data: newName//['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
                    }],
                    yAxis: [{
                        type: 'value',
                        axisTick: {
                            show: false
                        },
                        axisLine: {
                            lineStyle: {
                                color: '#57617B'
                            }
                        },
                        axisLabel: {
                            margin: 10,
                            textStyle: {
                                fontSize: 14
                            }
                        },
                        splitLine: {
                            lineStyle: {
                                color: '#57617B'
                            }
                        }
                    }],
                    series: [{
                        name: T.T("UserAccess"),
                        type: 'line',
                        smooth: true,
                        lineStyle: {
                            normal: {
                                width: 1
                            }
                        },
                        areaStyle: {
                            normal: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                    offset: 0,
                                    color: 'rgba(137, 189, 27, 0.3)'
                                }, {
                                    offset: 0.8,
                                    color: 'rgba(137, 189, 27, 0)'
                                }], false),
                                shadowColor: 'rgba(0, 0, 0, 0.1)',
                                shadowBlur: 10
                            }
                        },
                        itemStyle: {
                            normal: {
                                color: 'rgb(137,189,27)'
                            }
                        },
                        data: newData//[196, 196, 197, 195, 0, 194, 189, 194, 280, 152, 75, 294]
                    }, {
                        name: T.T("RegistrationAmount"),
                        type: 'line',
                        smooth: true,
                        lineStyle: {
                            normal: {
                                width: 1
                            }
                        },
                        areaStyle: {
                            normal: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                    offset: 0,
                                    color: 'rgba(137, 189, 27, 0.3)'
                                }, {
                                    offset: 0.8,
                                    color: 'rgba(137, 189, 27, 0)'
                                }], false),
                                shadowColor: 'rgba(0, 0, 0, 0.1)',
                                shadowBlur: 10
                            }
                        },
                        itemStyle: {
                            normal: {
                                color: 'rgb(255,140,0)'
                            }
                        },
                        data: registerData//[196, 196, 197, 195, 0, 194, 189, 194, 280, 162, 75, 294]
                    }]
                };

                if (option && typeof option === "object") {
                    myChart.setOption(option, true);
                }
            } else {   // 获取失败 
                Msg.error(T.T("DataAccessFailed"))
            }
        }, function errorCallback() {
            //console.log(T.T("RequestException"));
        });
    }

    //请求
    $scope.initFun = function () {
        $scope.GetTopTeacher();
        $scope.GetTopCourse();
        $scope.GetTopOrg();
        $scope.GetAllTopActivity();
    }

    //初始
    $scope.initFun();

} ]);